package de.fhaugsburg.games.boardgames.scrabble;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

import de.fhaugsburg.games.boardgames.CircularList;
import de.fhaugsburg.games.boardgames.PlayerMode;
import de.fhaugsburg.games.boardgames.scrabble.managment.GridPoint;
import de.fhaugsburg.games.boardgames.scrabble.managment.ScrabbleRandomPool;
import de.fhaugsburg.games.boardgames.scrabble.managment.WordManager;

public class DynProxy implements InvocationHandler {
	private IScrabbleEngine engine;
	private Logger logger;
	FileHandler handler;

	/**
	 * @directed
	 */

	public DynProxy(IScrabbleEngine engine) throws Exception {

		this.engine = engine;
		logger = Logger.getLogger("Scrabblelogger");
		handler = new FileHandler("Scrabblelog.txt");
		logger.addHandler(handler);
		logger.setLevel(Level.ALL);
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		if (args != null) {
			//System.out.print("* calling method " + method + " with params ");
			for (int i = 0; i < args.length; i++) 
			{
				logger.fine(" " + args[i]);
			//	System.out.print(" " + args[i]);
				//System.out.println();
			}
			logger.fine("* calling method " + method + " with params ");

		}

		Object result = null;

		try {
			logger.fine(method.getName());
			result = method.invoke(engine, args);
		} catch (IllegalAccessException ex) {
			logger.warning(ex.getMessage());
			// ex.printStackTrace();
		} catch (InvocationTargetException ex) {
			logger.warning(ex.getMessage());
			//System.out.println("* exception:" + ex.getTargetException());
			// throw ex.getTargetException();
		}
	//	System.out.println("* result:" + result);
		return result;
	}

}
